package com.learn._3stack;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;

public class Calculator {

    static Set<Character> operaters =  new HashSet<>();

    static{
        operaters.add('+');
        operaters.add('-');
        operaters.add('*');
        operaters.add('/');
        operaters.add('(');
        operaters.add(')');
    }



    public static void main(String[] args) {


        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        try {
            String input = bufferedReader.readLine();

            SimpleStack<Double> numberStack = new SimpleStack();
            SimpleStack<Character> operatorStack = new SimpleStack();

            int tmp = 0;
            for(int i = 0;i < input.length();i ++){
                char c = input.charAt(i);
                if(operaters.contains(c)){
                    //説明這個符號前是數字
                    if(tmp != i) {
                        String number = input.substring(tmp, i);
                        Double num = Double.parseDouble(number);
                        numberStack.push(num);
                    }
                    if(c == '('){
                        //认为优先级最大
                        operatorStack.push(c);

                    }else if(c == ')'){
                        //认为优先级最小，不断计算前面的值，直到碰到左括号
                        c = operatorStack.pop();
                        while(c != '('){
                            Double secondNum = numberStack.pop();
                            Double firstNum = numberStack.pop();

                            Double res = doCalculate(firstNum,secondNum,c);
                            numberStack.push(res);
                            c = operatorStack.pop();
                        }
                    }else{
                        if(operatorStack.size() > 0){
                            Character preOperater = operatorStack.peek();
                            if(getPriority(c) > getPriority(preOperater)){
                                //当前操作符优先级大于前面的操作符，不操作，直接入栈
                                operatorStack.push(c);
                            }else{
                                //将前面的操作符进行计算
                                char o = operatorStack.pop();
                                Double secondNum = numberStack.pop();
                                Double firstNum = numberStack.pop();

                                Double res = doCalculate(firstNum,secondNum,o);
                                numberStack.push(res);
                                operatorStack.push(c);
                            }
                        }else{
                            operatorStack.push(c);
                        }
                    }
                    tmp = i + 1;
                }else if(i == input.length() - 1){
                    //字符串最後一個如果不是字符符号结尾，则有一个数字没有加到栈里，处理这个数字
                    String number = input.substring(tmp, i + 1);
                    Double num = Double.parseDouble(number);
                    numberStack.push(num);
                }
            }

            //最后可能有遗留的，处理掉
            while(numberStack.size() > 1){
                char o = operatorStack.pop();
                Double secondNum = numberStack.pop();
                Double firstNum = numberStack.pop();

                Double res = doCalculate(firstNum,secondNum,o);
                numberStack.push(res);
            }


            System.out.println("结果为："+numberStack.pop());



        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    private static int getPriority(char c) {
        if(c == '+' || c == '-'){
            return 1;
        }else if (c == '*' || c == '/'){
            return 2;
        }
        return 0;
    }

    private static Double doCalculate(Double firstNum, Double secondNum, char c) {

        if(c == '+'){
            return  firstNum + secondNum;
        }else if(c =='-'){
            return firstNum - secondNum;
        }else if(c == '*'){
            return firstNum * secondNum;
        }else if(c == '/'){
            return firstNum / secondNum;
        }
        return 0d;

    }


}
